package com.lzh.eventdemo.service;

import com.lzh.eventdemo.event.UserRegisterEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description: 通过实现ApplicationListener 监听事件
 * @Author: LiuZiHao
 * @Date: 2022/8/30 10:41
 */
@Service
public class EmailService implements ApplicationListener<UserRegisterEvent> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    @Async //设置 @Async 注解，声明异步执行。毕竟实际场景下，发送邮件可能比较慢，又是非关键逻辑。
    public void onApplicationEvent(UserRegisterEvent event) {
        logger.info("[onApplicationEvent][给用户({}) 发送邮件]", event.getUsername());
    }
}
